package com.project.service.impl;

import com.project.bean.TradeBean;
import com.project.bean.UserBean;
import com.project.dao.ITradeDao;
import com.project.dao.IUserDao;
import com.project.dao.impl.TradeDaoImpl;
import com.project.dao.impl.UserDaoImpl;
import com.project.service.IUserService;

public class UserServiceImpl implements IUserService {
    private IUserDao  userDao = new UserDaoImpl();
    private ITradeDao tradeDao = new TradeDaoImpl();

    @Override
    public UserBean login(String userName, String password) {
        UserBean userBean =userDao.login(userName,password);

        return userBean;
    }

    @Override
    public void setMoney(String code, double account) {

        userDao.update(code,account);
        tradeDao.addInfo(new TradeBean("存入金额",account,code));
    }

    @Override
    public boolean drawMoney(String code, double account) {
        UserBean userBean= userDao.findByName(code);
        if (userBean.getAccount()<account){
            return false;
        }
        userDao.update(code,-account);
        tradeDao.addInfo(new TradeBean("取出金额",account,code));
        return true;
    }

    @Override
    public String transferMoney(String code, String targetCode, double account) {
        UserBean user = userDao.findByName(code);
        UserBean targetUser = userDao.findByName(targetCode);
        if (targetUser == null) {
            return "目标账户不存在";
        }
        if (user.getAccount() < account) {
            return "余额不足";
        }
        userDao.update(code, -account);
        userDao.update(targetCode, account);
        tradeDao.addInfo(new TradeBean( "转账给" + targetCode, account,code));
        tradeDao.addInfo(new TradeBean(code + "转入", account,targetCode));
        return "转账成功";
    }

    @Override
    public UserBean queryMoney(String code) {
        UserBean userBean = userDao.findByName(code);

        return userBean;
    }


}
